package com.one.efaimaly.order.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.common.order.AddMaterialItem;
import com.one.common.config.RouterPath;
import com.one.common.manager.event.BusManager;
import com.one.common.model.extra.BaseExtra;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.view.base.BaseFragment;
import com.one.common.view.base.BaseFragmentActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.efaimaly.R;
import com.one.efaimaly.order.model.event.AddMaterialEvent;
import com.one.efaimaly.order.model.extra.MaterialFragmentExtra;
import com.one.efaimaly.order.presenter.AddMaterialPresenter;
import com.one.efaimaly.order.ui.fragment.AddMFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * creator: zhulunjun
 * time:    2018/8/15
 * describe: 添加材料
 */
@Route(path = RouterPath.ADD_MATERIAL)
public class AddMActivity extends BaseFragmentActivity<AddMaterialPresenter> {

    @BindView(R.id.tv_select_pay)
    TextView tvSelectPay;
    @BindView(R.id.tv_select_free)
    TextView tvSelectFree;
    @BindView(R.id.tv_btn_confirm)
    TextView tvBtnConfirm;
    private AddMFragment freeFragment;
    private AddMFragment payFragment;

    @Override
    protected ArrayList<BaseFragment> getFragments() {
        Bundle freeBundle = new Bundle();
        freeBundle.putSerializable(BaseExtra.getExtraName(),
                new MaterialFragmentExtra(AddMaterialEvent.FREE, mPresenter.getFreeItems(), mPresenter.isUpdate()));
        freeFragment = new AddMFragment();
        freeFragment.setArguments(freeBundle);
        Bundle payBundle = new Bundle();
        payBundle.putSerializable(BaseExtra.getExtraName(),
                new MaterialFragmentExtra(AddMaterialEvent.PAY, mPresenter.getPayItems(), mPresenter.isUpdate()));
        payFragment = new AddMFragment();
        payFragment.setArguments(payBundle);

        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(freeFragment);
        fragments.add(payFragment);
        return fragments;
    }

    @Override
    protected int getContainerViewId() {
        return R.id.fl_material;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new AddMaterialPresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_add_material;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("确认订单");
    }

    @Override
    public void initView() {
        super.initView();
        tvSelectPay.setOnClickListener(this);
        tvSelectFree.setOnClickListener(this);
        tvBtnConfirm.setOnClickListener(this);
        changePage(1);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_select_free:
                tvSelectPay.setTextColor(getResources().getColor(R.color.text_color_gray_80));
                tvSelectFree.setTextColor(getResources().getColor(R.color.text_color_black));
                changePage(0);
                break;
            case R.id.tv_select_pay:
                tvSelectPay.setTextColor(getResources().getColor(R.color.text_color_black));
                tvSelectFree.setTextColor(getResources().getColor(R.color.text_color_gray_80));
                changePage(1);
                break;
            case R.id.tv_btn_confirm:
                mPresenter.setFreeItems(freeFragment.getMaterialList());
                mPresenter.setPayItems(payFragment.getMaterialList());
                mPresenter.confirmAddMaterials();
                break;
            default:
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            DefaultExtra defaultExtra = (DefaultExtra) data.getSerializableExtra(DefaultExtra.getExtraName());
            BusManager.getBus().post(new AddMaterialEvent(defaultExtra.getNum(), defaultExtra.getStr()));

        }
    }

    private GetMListDataListener getMListDataListener;

    public void setGetMListDataListener(GetMListDataListener getMListDataListener) {
        this.getMListDataListener = getMListDataListener;
    }

    public interface GetMListDataListener {
        List<AddMaterialItem> getData();
    }

}
